#!/usr/bin/python
from urllib import urlopen
from ServerVersion import GetApacheVersion,GetIISVersion,GetNginxVersion
from FrameVersion import GetPHPVersion,GetASPNETVersion
from AppIdentify import IdentifyApp
import re

class InfoGather(object):
    def __init__(self,url):
        self.url=url
        
    def gather(self):
        #Gather the target website information such as web server,technology and web application
        #@argument:url
        #@print: server,technology,web application
        print "[--Information Gather--]"
        response=urlopen(self.url)
        self.headers=response.info()
        
        self.getServer()
        self.getFrame()
        self.getApp()
        
    def getServer(self):
        server_list={'apache':"GetApacheVersion",'nginx':"GetNginxVersion","iis":"GetIISVersion"}
        try:
            server=self.headers.getheader('Server')
            print "Server:",server
            for (name,ver) in server_list.items():
                if server.lower().find(name)!=-1:
                    version=eval(ver)()
                    if name=="apache" or name=="nginx":
                        reg=re.compile(r'\d+\.\d+\.\d+')
                    else:
                        reg=re.compile(r'\d+\.\d+')
                    try:
                        org_num=reg.search(server).group()
                        late_num=reg.search(version).group()
                        if cmp(late_num,org_num)>0:
                            print "--Warning: Your Webserver is not the latest stable version,please update to version",late_num," in time."
                        elif cmp(late_num,org_num)==0:
                            print "--Info:Your Webserver is latest version",late_num,",Good!"
                        else:
                            print "--Info:Your Webserver version is ahead of the latest stable version."
                        break
                    except:
                        pass
        except:
            print "Server: Unknown"
            
    def getFrame(self):
        frame_list={'php':"GetPHPVersion",'asp.net':"GetASPNETVersion"}
        try:
            frame=self.headers.getheader("X-Powered-By")
	    if frame:
            	print "Technology:", frame
            if frame.lower().find('php')!=-1:
                version=eval(frame_list['php'])()
                reg=re.compile(r"\d+\.\d+\.\d+")
                try:
                    org_num=reg.search(frame).group()
                    late_num=reg.search(version).group()
                    if cmp(late_num,org_num)>0:
                        print "--Warning: Your PHP is not the latest stable version,please update to version",late_num,"in time"
                    elif cmp(late_num,org_num)==0:
                        print "--Info: Your PHP is the latest stable version,Good!"
                    else:
                        print "--Info: Your PHP is ahead of the latest version."
                except:
                    pass
        except:
            print "Technology: Unknown :("
            
    def getApp(self):
        try:
            app=IdentifyApp(self.url)
            if app==None:
                app="Unknown :("    
        except:
            app= "Unknown :("
        print "Web Application:",app    
